

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layuiAdmin 栏目管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<style>
.layui-icon>span{
	    float: left;
    width: 32px;
    height: 32px;
    color: white;
    background: #393d49;
    margin-left: 10px;
    text-align: center;
    line-height: 32px;
	cursor:pointer;
}
.layui-icon>span.active{background: #1E9FFF;}
</style>
<body>
  <div class="layui-fluid">   
    <div class="layui-card">
    
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
          <button class="layui-btn layuiadmin-btn-role layuiadmin-btn-useradmin" data-type="batchdel">删除</button>
          <button class="layui-btn layuiadmin-btn-role layuiadmin-btn-useradmin" data-type="add">添加</button>
        </div>

        <table id="LAY-user-back-role" lay-filter="LAY-user-back-role"></table>
      
      </div>
    </div>
  </div>

 <script src="../../../layuiadmin/layui/layui.js"></script>
   <script src="../../../layuiadmin/layui/layui.all.js"></script>  
   <script src="../../../layuiadmin/config.js"></script> 
  <script>
    layui.use(['table'], function(){
      var $ = layui.$
              ,form = layui.form
              ,table = layui.table;
      var tableIns = table.render({
        elem: '#LAY-user-back-role',
        url : "{:url('lists')}",
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,40,100,500,1000],
        limit : 40,
        id : "LAY-user-manage",
        cols : [[
          {checkbox: true},
          {field: 'id', title: 'ID', minWidth:100, align:"center"},
          
          {field: '', title: '栏目名称', align:'center',minWidth:80,templet:function(d){
        	  return '<a class=" layui-btn-normal layui-btn-xs layui-btn layuiadmin-btn-useradmin" data-id="'+d.id+'" data-type="next">'+d.title+'</a>';
          }},
          {field: 'name', title: '栏目标识', align:'center'},
          {field: 'sort', title: '排序', align:'center',minWidth:80},
          {field: '', title: '状态', align:'center',minWidth:50,templet:function(d){
        	  if(d.status==1){return '<span class="label label-success radius">启用</span>';}
        	  else{return '<span class="label label-warning radius">禁用</span>';}
          }},
          {field: '', title: '操作', align:'center',templet:function(d){
            return '<a class="layui-btn layui-btn-normal layui-btn-xs layui-btn layuiadmin-btn-useradmin" data-id="'+d.id+'" data-title="'+d.title+'" data-p_title="'+d.p_title+'" data-name="'+d.name+'" data-pid="'+d.pid+'" data-status="'+d.status+'" data-sort="'+d.sort+'" data-type="add" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>'+
                    '<a class="layui-btn layui-btn-danger layui-btn-xs layui-btn layuiadmin-btn-useradmin" data-id="'+d.id+'" data-type="batchdel" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>';
          }},
        ]]
      });
      //监听搜索
      form.on('submit(LAY-user-front-search)', function(data){
        var field = data.field;
        console.log(field);
        //执行重载
        table.reload('LAY-user-manage',{
          url: "{:url('lists?op=table')}",
          where : field,
          done: function(res, curr, count){
            //如果是异步请求数据方式，res即为你接口返回的信息。
            //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
            console.log(res);
            //得到当前页码
//                console.log(curr);
            //得到数据总量
//                console.log(count);
          }
        });
      });

      //事件
      var active = {
    	next:function(){
    		 table.reload('LAY-user-manage',{
    	          url: "{:url('lists?op=table')}",
    	          where : {pid:$(this).data('id')},
    	          done: function(res, curr, count){
    	            //如果是异步请求数据方式，res即为你接口返回的信息。
    	            //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
    	            console.log(res);
    	            //得到当前页码
//    	                console.log(curr);
    	            //得到数据总量
//    	                console.log(count);
    	          }
    	        });
    	}	  
        ,batchdel: function(){
          var checkStatus = table.checkStatus('LAY-user-manage')
                  ,checkData = checkStatus.data; //得到选中的数据
                 
          if(checkData.length === 0){
            return layer.msg('请选择数据');
          }
                  ajax_data = [];
		$.each(checkData,function(k,v){
			ajax_data.push(v.id);
		})
          layer.confirm('确定删除栏目及其子栏目吗？', function(index) {
              console.log(ajax_data)
              $.ajax({
            		type: 'POST',
    	            url: '{:url("del")}',
    	            data: {id:ajax_data},
    	            dataType: 'json',
    	            //timeout: 3000,
    	            success: function (data) {
    	         	   console.log(data)
    	         	   if(data.code==0){
    	         		  table.reload('LAY-user-manage'); //数据刷新
    	                  layer.closeAll(); //关闭弹层
    	                  layer.msg(data.msg);
    	         	   }else{
    	         		  layer.msg(data.msg);
    	         	   }
    	            }
            	});
            });
        }
        ,add: function(){
        	parents_menu = {$parents_menu};
        	str_menu = '';
        	id = $(this).data('id');title = $(this).data('title');name = $(this).data('name');
        	pid = $(this).data('pid');sort = $(this).data('sort');status = $(this).data('status');
        	p_title = $(this).data('p_title');
            home_icon = (p_title=='home')?'active':'';
            user_icon = (p_title=='user')?'active':'';
            form_icon = (p_title=='form')?'active':'';
            set_icon = (p_title=='set')?'active':'';
            list_icon = (p_title=='list')?'active':'';
            senior_icon = (p_title=='senior')?'active':'';
            app_icon = (p_title=='app')?'active':'';
            dialogue_icon = (p_title=='dialogue')?'active':'';
        	status1 = (status=='0')?'':'checked';
        	status2 = (status=='0')?'checked':'';
        	if(id){
        		this_url = 'edit/id/'+id+'.html';
        	}else{
        		this_url = '{:url("add")}';
        	}
        	console.log(this_url)
        	$.each(parents_menu,function(k,v){
        		check = '';
        		if(pid==v.id){
        			check = 'selected';
        		}
        		str_menu +='<option '+check+' value="'+v.id+'">'+v.title+'</option>';
        		$.each(v.son,function(ka,va){
        			check = '';
            		if(pid==va.id){
            			check = 'selected';
            		}
        			str_menu +='<option '+check+' value="'+va.id+'">|--'+va.title+'</option>';
        			$.each(va.son,function(kaa,vaa){
        				check = '';
                		if(pid==vaa.id){
                			check = 'selected';
                		}
            			str_menu +='<option '+check+' value="'+vaa.id+'">&nbsp;&nbsp;&nbsp;|--'+vaa.title+'</option>';
            		})
        		})
        	})
        	
          content = '<div class="layui-form" lay-filter="layuiadmin-form-role" id="layuiadmin-form-role" style="padding: 20px 30px 0 0;">'+
        '<form id="form1"><div class="layui-form-item">'+
          '<label class="layui-form-label">栏目名称</label>'+
         ' <div class="layui-input-block">'+
          '  <input type="text" name="title" value="'+data_null(title)+'" class="layui-input">'+
          '</div>'+
       ' </div>'+
       '<div class="layui-form-item">'+
       '<label class="layui-form-label">栏目标识</label>'+
      ' <div class="layui-input-block">'+
       '  <input type="text" name="name" value="'+data_null(name)+'" class="layui-input">'+
       '</div>'+
    ' </div>'+
    '<div class="layui-form-item">'+
    '<label class="layui-form-label">栏目图标</label>'+
   ' <div class="layui-input-block layui-icon">'+
   '  <input type="hidden" name="p_title" id="p_title" value="'+data_null(p_title)+'"/>'+
   '  <span class="'+home_icon+'"><i data-icon="home" class="layui-icon layui-icon-home"></i></span><span class="'+user_icon+'"><i data-icon="user" class="layui-icon layui-icon-user"></i></span>'+
   '  <span class="'+list_icon+'"><i data-icon="list" class="layui-icon layui-icon-list"></i></span><span class="'+app_icon+'"><i data-icon="app" class="layui-icon layui-icon-app"></i></span>'+
   '  <span class="'+senior_icon+'"><i data-icon="senior" class="layui-icon layui-icon-senior"></i></span class="'+form_icon+'"><span><i data-icon="form" class="layui-icon layui-icon-form"></i></span>'+
   '  <span class="'+set_icon+'"><i data-icon="set" class="layui-icon layui-icon-set"></i></span><span class="'+dialogue_icon+'"><i data-icon="dialogue" class="layui-icon layui-icon-dialogue"></i></span>'+
    '</div>'+
   ' </div>'+
    '<div class="layui-form-item">'+
    '<label class="layui-form-label">上级栏目</label>'+
   ' <div class="layui-input-block">'+
    '<select class="layui-select" style="display:block;width:100%;" name="pid"><option value="0">顶级菜单</option>'+str_menu+'</select>'+
    '</div>'+
 ' </div>'+
 '<div class="layui-form-item">'+
 '<label class="layui-form-label">栏目顺序</label>'+
' <div class="layui-input-block">'+
 '  <input type="text" name="sort" value="'+data_null(sort)+'" class="layui-input">'+
 '</div>'+
' </div>'+
'<div class="layui-form-item">'+
'<label class="layui-form-label">是否启用</label>'+
' <div class="layui-input-block" style="line-height:36px;">'+
'<input type="radio" '+status1+' value="1" style="display:inline-block;/* height:36px;line-height:36px; */" name="status" ><span style="">是</span>'+
'<input type="radio" '+status2+' value="0" style="display:inline-block;/* height:36px;line-height:36px; */" name="status" ><span style="">否</span>'+
'</div>'+
' </div>'+
'</form>'+
     ' </div>';
          layer.open({
            type: 1
            ,title: '添加栏目'
            ,content: content
            ,maxmin: true
            ,area: ['500px', 'auto']
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
            	$.ajax({
            		type: 'POST',
    	            url: this_url,
    	            data: $('#form1').serialize(),
    	            dataType: 'json',
    	            timeout: 3000,
    	            success: function (data) {
    	         	   console.log(data)
    	         	   if(data.code==0){
    	         		  table.reload('LAY-user-manage'); //数据刷新
    	                  layer.closeAll(); //关闭弹层
    	                  layer.msg(data.msg);
    	         	   }else{
    	         		  layer.msg(data.msg);
    	         	   }
    	            }
            	});
            }
          });
        }
      };

      $('body').on('click','.layui-btn.layuiadmin-btn-useradmin', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
      });
      $('body').on('click','.layui-icon>span', function(){
    	      icon = $(this).children('i').data('icon');
    	      $('#p_title').val(icon);
              $(this).siblings('span').removeClass('active');
              $(this).addClass('active');
        });
    });

  </script>
</body>
</html>

